<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../../../lib/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body>
<!--<input type="hidden" name="rowId">-->
<div class="layuimini-container">

  <div class="layuimini-main">
    <fieldset class="table-search-fieldset" style="margin-bottom: 12px">
      <legend>搜索信息</legend>
      <div style="margin: 10px 10px 10px 10px">
        <form class="layui-form layui-form-pane" action="">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">姓名</label>
              <div class="layui-input-inline">
                <input type="text" name="username" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">登录名</label>
              <div class="layui-input-inline">
                <input type="text" name="sex" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">状态:</label>
              <div class="layui-input-block">
                <select>
                  <option value="1">正常</option>
                  <option value="2">禁用</option>
                  <option value="3">锁定</option>
                </select>
              </div>
            </div>
            <div class="layui-inline">
              <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i
                  class="layui-icon"></i> 搜 索
              </button>
            </div>
          </div>
        </form>
      </div>
    </fieldset>

    <script type="text/html" id="toolbarDemo">
      <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
      </div>
    </script>

    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    <input type="hidden" name="hiddenFieldForTable" id="hiddenFieldForTable">
    <script type="text/html" id="currentTableBar">
      <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
<!--      <a class="layui-btn layui-btn-warm layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">禁用</a>-->
      <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete" type="button">删除</a>
    </script>

  </div>
</div>
<script src="../../../lib/layui/layui.js" charset="utf-8"></script>
<script src="../../../api/common.js" charset="utf-8"></script>
<script>
  layui.use(['form', 'table'], function () {
    var $ = layui.jquery,
      form = layui.form,
      table = layui.table;

    var headers = {
      'Authorization': `Bearer ${ localStorage.getItem("token") }`,
    };


    table.render({
      elem: '#currentTableId',
      url: local + 'user/search/list',
      method: "post",
      contentType: 'application/json;charset=UTF-8',
      headers: {'Authorization': `Bearer ${ localStorage.getItem("token") }`},
      request: {
        pageName: 'pageNum',
        limitName: 'pageSize'
      },
      toolbar: '#toolbarDemo',
      defaultToolbar: ['filter', 'exports', 'print'],
      cols: [[
        { type: 'numbers', title: '序号', width: 60, fixed: 'left' },
        {field: 'username', title: '登录名'},
        {field: 'realName', title: '姓名'},
        {field: 'userTel', title: '电话号码'},
        {field: 'orgName', title: '组织'},
        {field: 'roleName', title: '角色'},
        {field: 'userStatus', title: '状态'},
        {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center",fixed: 'right'}
      ]],
      done: function (res, curr, count) {
        // 如果是异步请求数据方式，res即为你接口返回的信息。
        // 如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
        $("[data-field='userStatus']").children().each(function () {
          if ($(this).text() == '0') {
            $(this).text('正常');
          } else if ($(this).text() == '1') {
            $(this).text('禁用');
          }else if ($(this).text() == '2') {
            $(this).text('锁定');
          }
        });
      },
      limits: [10, 15, 20, 25, 50, 100],
      limit: 10,
      page: true,
      unresize: false,
      skin: 'line',
      parseData: function(res){ // res 即为原始返回的数据
        return {
          "code": res.code === 200 ? 0 : res.code, // 解析接口状态
          "msg": res.message, // 解析提示文本
          "count": res.data.total, // 解析数据长度
          "data": res.data.records
          // 解析数据列表
        };
      },
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
      var result = JSON.stringify(data.field);
      layer.alert(result, {
        title: '最终的搜索信息'
      });

      //执行搜索重载
      table.reload('currentTableId', {
        page: {
          curr: 1
        }
        , where: {
          searchParams: result

        }
      }, 'data');

      return false;
    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
      if (obj.event === 'add') {  // 监听添加操作
        var index = layer.open({
          title: '添加账户',
          type: 2,
          shade: 0.2,
          maxmin: true,
          shadeClose: true,
          area: ['50%', '50%'],
          content: './add.html',
        });
        $(window).on("resize", function () {
          layer.full(index);
        });
      }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
      console.log(obj)
    });

    table.on('tool(currentTableFilter)', function (obj) {
      var data = obj.data;
      if (obj.event === 'edit') {
        var index = layer.open({
          title: '编辑账户',
          type: 2,
          shade: 0.2,
          maxmin: true,
          shadeClose: true,
          data: { id: '1234' },
          area: ['50%', '50%'],
          content: './edit.html?id=' + data.userId,
        });
        $(window).on("resize", function () {
          layer.full(index);
        });
        return false;
      } else if (obj.event === 'delete') {
        console.log('data', data)
        $.ajax({
          type: 'POST',
          url: local + 'user/delete/' + data.userId,
          data: {},
          dataType: "json",
          headers: {
            'Authorization': `Bearer ${ localStorage.getItem("token") }`,
          },
          contentType: 'application/json;charset=UTF-8',
          success: function (res) {
            if (res.code === 200) {
              layui.table.reload('currentTableId')
            }
          },
          error: function (XMLHttpRequest, textStatus, errorThrown) {
            layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, {icon: 5});
          }
        });
      }
    });

  });
</script>

</body>
</html>
